package bit.book.controller;

import bit.book.constant.Constants;
import bit.book.entity.*;
import bit.book.enums.BookStatus;
import bit.book.enums.ResultCodeEnum;
import bit.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/book",produces = "application/json")
public class BookController {
    @Autowired
    BookService bookService = new BookService();
    private static final Logger logger = LoggerFactory.getLogger(BookService.class);
    @RequestMapping("/getList")
    public List<BookInfo>getList(){
        return bookService.getList();
    }

    @RequestMapping("/addBook")
//    public String addBook(BookInfo book){
    public Result addBook(BookInfo book){
        log.info("添加图书，request:"+book);
        if(!StringUtils.hasLength(book.getBookName())||
                book.getCount()==null||
                book.getPrice()==null||
                !StringUtils.hasLength(book.getPublish())||
                book.getStatus()==null) {
            log.error("参数不合法 e:{}"  ,book);//打印堆栈要加大括号
//            return "参数不合法";
            return Result.fail("参数不合法");
        }

        try{
            bookService.addBook(book);
//            return "";
            return Result.success("");
        }catch(Exception e){
            log.error("添加图书异常,e:",e);
//            return "error";
            return Result.fail("error");
        }
    }


    @RequestMapping("/getListByPage")
     public Result<ResponsePage<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
//
//        if(session.getAttribute(Constants.SESSION_USER_KEY)==null)
//            return Result.unlogin();
//        UserInfo userInfo=(UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo==null||userInfo.getId()<=0){
////            Result result=new Result();
////            result.setCode(ResultCodeEnum.UNLOGIN);
////            result.setErrMsg("用户未登录");
////            result.setData(null);
////            return result;
//
//            return Result.unlogin();
//
////            return new ResponsePage<>();
//        }

        //还可以改进，最好把处理逻辑放到service
        ResponsePage<BookInfo> responsePage = new ResponsePage<>();
        responsePage.setBookList(bookService.getBookListByPage(pageRequest));
//         responsePage.setBookList(bookService.getBookListByPage(page,size));
         responsePage.setCount(bookService.countBookByPage());
         responsePage.setPageRequest(pageRequest);

//        Result result=new Result();
//        result.setCode(ResultCodeEnum.SUCCESS);
//        result.setErrMsg("");
//        result.setData(responsePage);
//        return result;
        return Result.success(responsePage);

//         return responsePage;
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){

        log.info("查询图书request:"+bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
//    public String updateBook(BookInfo book){
        public Result updateBook(BookInfo book){
        log.info("修改图书request:"+book);
        try {
            bookService.updateBook(book);
//            return "更新成功";
            return Result.success("更新成功");
        }catch (Exception e){
            log.error("更新失败,e:",e);
//            return "更新失败";
             return Result.fail("更新失败");
        }
    }

    @RequestMapping("/deleteBook")
//    public String deleteBook(Integer bookId){
    public Result deleteBook(Integer bookId){
        log.info("删除request:"+bookId);
        try {
            BookInfo bookInfo=new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatus.DELETE.getCode());
            bookService.deleteBook(bookInfo);
//            return "删除成功";
            return Result.success("删除成功");
        }catch (Exception e){
            log.error("删除失败,e:",e);
//            return "删除失败";
             return Result.fail("删除失败");
        }
    }

    @RequestMapping("batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除,ids:{}",ids);
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败，e:",e);
            return false;
        }



    }


}
